#!/usr/bin/python
#coding = utf-8

import os
import sys

if os.getuid() == 0:
	pass
else:
	print ('当前用户不是root用户，请以root用户执行脚本')
	sys.exit(1)

version = input('请输入你想安装的python的版本号（2.7/3.6）')
version = str(version)

if version == '2.7':
    url = 'https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz'
elif version == '3.6':
    url = 'https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz'
else:
    print('您输入的版本号有误（2.7/3.6）')
    sys.exit(1)

# 组装命令
cmd = 'wget' + url

res = os.system(cmd)

if res != 0:
    print('下载源码包失败，请检查当前网络')
    sys.exit(1)


if version == '2.7':
    packge_name = 'Python-2.7.12'
else:
    packge_name = 'Python-3.6.4'
cmd = 'tar xf ' + packge_name + 'tar.tgz'

res = os.system(cmd)
if res != 0:
    os.system('rm ' + packge_name + 'tar.tgz')
    print('解压源码包失败，请重新运行该脚本')
    sys.exit(1)

cmd = 'cd ' + packge_name + ' && ./confiture --prefix=/usr/local/python && make && make install'
res = os.system(cmd)
if res != 0:
    print('编译Python源码失败，请检查是否缺少依赖库')
    sys.exit(1)